package com.xkstream.client.ui.live;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.TextView;

import com.xkstream.client.R;
import com.xkstream.client.application.Application;
import com.xkstream.client.base.BaseActivity;
import com.xkstream.client.base.BaseData;
import com.xkstream.client.entity.ReceiveGift;
import com.xkstream.client.entity.User;
import com.xkstream.client.entity.kt.GiftRankingData;
import com.xkstream.client.presenter.GiftContributionContract;
import com.xkstream.client.presenter.GiftContributionPresenter;
import com.xkstream.client.ui.adapter.GiftContributionAdapter;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/4/11.
 */

public class GiftContributionActivity extends BaseActivity<GiftContributionContract.Presenter>
        implements GiftContributionContract.View {
    @BindView(R.id.tvWeek)
    TextView tvWeek;
    @BindView(R.id.tvMonth)
    TextView tvMonth;
    @BindView(R.id.tvAll)
    TextView tvAll;
    @BindView(R.id.lvGiftRank)
    ListView lvGiftRank;
    private List<ReceiveGift> week = new ArrayList<>();
    private List<ReceiveGift> month = new ArrayList<>();
    private List<ReceiveGift> total = new ArrayList<>();

    private List<ReceiveGift> list = new ArrayList<>();

    private GiftContributionAdapter adapter;

    private Context context;


    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.layout_giftranking);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        context = this;
        presenter = new GiftContributionPresenter(this);
        adapter = new GiftContributionAdapter(list, context);
        lvGiftRank.setAdapter(adapter);
        setItemSelected(1);
        loadGiftRankingData();
    }

    private void loadGiftRankingData() {

        User user = Application.getUser();
        String id = user.getUser_code();
        presenter.getGiftRankingData(id);
    }

    @OnClick({R.id.tvWeek, R.id.tvMonth, R.id.tvAll})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvWeek:
                setItemSelected(0);
                break;
            case R.id.tvMonth:
                setItemSelected(1);
                break;
            case R.id.tvAll:
                setItemSelected(2);
                break;
        }
    }

    private void setItemSelected(int position) {
        switch (position) {
            case 0:
                tvWeek.setBackground(getResources().getDrawable(R.drawable.shape_giftlefttitle_bg));
                tvWeek.setTextColor(getResources().getColor(R.color.black));
                tvMonth.setTextColor(getResources().getColor(R.color.white));
                tvMonth.setBackground(null);
                tvAll.setTextColor(getResources().getColor(R.color.white));
                tvAll.setBackground(null);
                list.clear();
                list.addAll(week);
                adapter.notifyDataSetChanged();
                break;
            case 1:
                tvWeek.setBackground(null);
                tvWeek.setTextColor(getResources().getColor(R.color.white));
                tvMonth.setTextColor(getResources().getColor(R.color.black));
                tvMonth.setBackgroundColor(getResources().getColor(R.color.white));
                tvAll.setTextColor(getResources().getColor(R.color.white));
                tvAll.setBackground(null);
                list.clear();
                list.addAll(month);
                adapter.notifyDataSetChanged();
                break;
            case 2:
                tvWeek.setBackground(null);
                tvWeek.setTextColor(getResources().getColor(R.color.white));
                tvMonth.setTextColor(getResources().getColor(R.color.white));
                tvMonth.setBackground(null);
                tvAll.setTextColor(getResources().getColor(R.color.black));
                tvAll.setBackground(getResources().getDrawable(R.drawable.shape_giftrighttitle_bg));
                list.clear();
                list.addAll(total);
                adapter.notifyDataSetChanged();
                break;
        }
    }


    @Override
    public void setPresenter(GiftContributionContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void showGiftRankingData(BaseData<GiftRankingData> data) {
            if (data.getCode() == 1){
                total = data.getData().getTotal();
                week = data.getData().getWeek();
                month = data.getData().getMonth();
                setItemSelected(1);
            }else {
                showToast(data.getMsg());
            }
    }
}
